/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.salPlan.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.salPlan.domain.SalPlan;
import qs.crm.modules.salPlan.repository.SalPlanRepository;
import qs.crm.modules.salPlan.service.SalPlanService;
import qs.crm.modules.salPlan.service.dto.SalPlanDto;
import qs.crm.modules.salPlan.service.dto.SalPlanQueryCriteria;
import qs.crm.modules.salPlan.service.mapstruct.SalPlanMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-05-27
**/
@Service
@RequiredArgsConstructor
public class SalPlanServiceImpl implements SalPlanService {

    private final SalPlanRepository salPlanRepository;
    private final SalPlanMapper salPlanMapper;

    @Override
    public Map<String,Object> queryAll(SalPlanQueryCriteria criteria, Pageable pageable){
        Page<SalPlan> page = salPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(salPlanMapper::toDto));
    }

    @Override
    public List<SalPlanDto> queryAll(SalPlanQueryCriteria criteria){
        return salPlanMapper.toDto(salPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SalPlanDto findById(Long plaId) {
        SalPlan salPlan = salPlanRepository.findById(plaId).orElseGet(SalPlan::new);
        ValidationUtil.isNull(salPlan.getPlaId(),"SalPlan","plaId",plaId);
        return salPlanMapper.toDto(salPlan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SalPlanDto create(SalPlan resources) {
        return salPlanMapper.toDto(salPlanRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SalPlan resources) {
        SalPlan salPlan = salPlanRepository.findById(resources.getPlaId()).orElseGet(SalPlan::new);
        ValidationUtil.isNull( salPlan.getPlaId(),"SalPlan","id",resources.getPlaId());
        salPlan.copy(resources);
        salPlanRepository.save(salPlan);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long plaId : ids) {
            salPlanRepository.deleteById(plaId);
        }
    }

    @Override
    public void download(List<SalPlanDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SalPlanDto salPlan : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("销售机会编号", salPlan.getPlaChcId());
            map.put("计划日期", salPlan.getPlaDate());
            map.put("计划目标", salPlan.getPlaTodo());
            map.put("结果", salPlan.getPlaResul());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}